//
//  TTNearbyStationsViewController.m
//  TrainTrack
//
//  Created by Jonathan Bunde-Pedersen on 06/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "TTNearbyStationsViewController.h"
#import "TrainTrackAppDelegate.h"
#import "Station.h"

@implementation TTNearbyStationsViewController

@synthesize navigationController, stations, departuresController, currentCell;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
	    self.tableView.delegate = self;
	    self.tableView.dataSource = self;
	    self.tableView.rowHeight = 50;
	    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
	    self.tableView.sectionHeaderHeight = 0;
	    self.tableView.multipleTouchEnabled = YES;
	    self.tableView.delaysContentTouches = YES;
	    self.tableView.scrollEnabled = YES;
	    		
		self.departuresController = [[TTDepaturesViewController alloc] initWithStyle:UITableViewStyleGrouped];
		
		[TTCoreLocationController sharedInstance].delegate = self;
		[Station findAll];
		self.stations = [NSArray array];
	    [self.tableView reloadData];
	}
	return self;
}


- (void)viewDidLoad {
	[super viewDidLoad];
	
	[TTCoreLocationController sharedInstance].delegate = self;
	NSLog(@"Adding CL delegate");
}

- (void)viewWillAppear:(BOOL)animated {
	[TTCoreLocationController sharedInstance].delegate = self;
	[[TTCoreLocationController sharedInstance].locationManager startUpdatingLocation];
	NSLog(@"starting location update");
	
	NSIndexPath *selection = [self.tableView indexPathForSelectedRow];
	if (selection) {
		[self.tableView deselectRowAtIndexPath:selection animated:YES];	
	}
}

// stop core location updates when view disappears
- (void)viewWillDisappear:(BOOL)animated {
	// Ask core location to stop updating the location
	NSLog(@"stopping location update");
	[[TTCoreLocationController sharedInstance].locationManager stopUpdatingLocation];	
}

// Core Location updates

-(void)newLocationUpdate:(CLLocation *)newLocation {
	// TESTING 
	// for testing purposes you can set your current location
	// to where you are now, or anywhere else if you please
	// this is to work around the simulators stupid standard response
	// You must not enable this in checked in code!
//		CLLocationDegrees latitude = 55.699925;
//		CLLocationDegrees longitude = 12.588487;
//		newLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
	// TESTING
	
	
	// Find all stations sorted by distance to the current location
	self.stations = [Station findAllSortedByDistanceToLocation:newLocation];
	[self.tableView reloadData];
}

-(void)newError:(NSString *)text {
}

// 
// TABLECONTROLLER
//

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.stations count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"TTNearbyCell";
	
	TTNearbyStationsCell *cell = (TTNearbyStationsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		NSArray *MyCellNib = [[NSBundle mainBundle] loadNibNamed:@"NearbyStationsCell" owner:self options:nil];
		cell = self.currentCell;
		MyCellNib = nil;
	}
	
	// Set up the cell
	Station *station = [self.stations objectAtIndex:indexPath.row];
	cell.stationLabel.text = station.name;
	cell.distanceLabel.text = station.distanceFromCurrentLocationText;
	
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	// Navigation logic -- create and push a new view controller
	
	// push new view onto navigationcontroller
	self.departuresController.station = [[self.stations objectAtIndex:indexPath.row] name];
	[self.navigationController pushViewController:self.departuresController animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

@end
